<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" tppabs="static/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="flash">刷新</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="upd">恢复</a>
</script>

<script src="../../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;
        var $=layui.$;
        table.render({
            elem: '#test'
            ,url:'/adminquery2'
            ,method: 'post'
            ,page: true
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,id:'testReload'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'aid', title:'ID', width:60,  unresize: true, sort: true}
                ,{field:'auserId',title:'用户编号', width:200}
                ,{field:'aposition', title:'角色', width:150,
                    templet: function (da){
                        if(da.aposition == 0){
                            return "超级管理员";
                        }else if(da.aposition == 1){
                            return "管理员";
                        }else if(da.aposition == 2){
                            return "员工";
                        }
                    }

                } //edit: 'text'   开启页面修改
                ,{field:'auser', title:'账号', width:100}
                ,{field:'apassword', title:'密码', width:100}
                ,{field:'aname', title:'姓名', width:100}
                ,{field:'asex', title:'性别', width:100}
                ,{field:'aphone', title:'手机号', width:150}
                ,{field:'atime', title:'注册时间', width:140,
                    templet: "<div>{{layui.util.toDateString(d.atime,'yyyy-MM-dd')}}</div>"
                }
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:120}
            ]]
            ,height: document.documentElement.clientHeight - 100
            // , parseData: function (rs) {//数据格式解析
            //     //console.log(rs);
            //     if (rs.code === 0) {
            //         return {
            //             "code": rs.code,
            //             "msg": rs.msg,
            //             "count": rs.data.totalCount,
            //             "data": rs.data.list
            //         }
            //     }
            // }
        });
        //监听头部工具栏
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'flash':
                    table.reload('testReload');
                    break;
            }
            ;
        })
        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'upd'){
                layer.confirm('真的恢复吗', function(index){
                    $.ajax({
                        type: "POST",
                        url: "/adminupdate3",
                        data: {"aid": data.aid},
                        dataType:"json",
                        success: function (da) {
                            if (da.code === 0) {
                                layer.msg("恢复成功！")
                                obj.del(); //删除对应行（tr）的DOM结构
                                // location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>

</html>